#include "OldGuy.h"



OldGuy::OldGuy(QGraphicsScene *scene)
{
    root = QFileInfo(__FILE__).absolutePath();
    shootspeed = 5;
    shootrecord = 0;
    // 初始化弹夹
    for(int i=0;i<20;i++)
        bulletlist.append(new Bullet(scene));

    // 用来存储小人走路三种状态的图片
    state = {{"1.png", "2.png", "3.png"},
             {"4.png", "5.png", "6.png"}};
    state_index1 = 0;  // 记录小人面向的方向
    state_index2 = 0;  // 记录小人当前处于哪一种移动状态
    state_time = 0;
    // 死亡动画图片
    for(int i=0;i<6;i++){
        deathlist.append(QPixmap(root + "/res/old_death_frame" + QString::number(i + 1) + ".png"));
        deathbloodlist.append(QPixmap(root + "/res/doom_splat_frame" + QString::number(i + 1) + ".png").scaled(200, 140));
    }

    isdeath = 0;    // 是否死亡
    position = QPointF(0, 0);
    rotate = 0;
    i = 0;  // 更新deathlist下标
    pix = QPixmap(root + "/res/old_guy_frame" + state[state_index1][state_index2]).scaled(85, 95);
    oldguydeadbody = QPixmap(root + "/res/old_guy_frame7.png").scaled(85, 95);
    this->setPos(-100,-100);
    this->setVisible(false);
    scene->addItem(this);
}

QRectF OldGuy::boundingRect() const{
    return QRectF(-pix.width()*0.5,-pix.height()*0.5,pix.width(),pix.height());
}

void OldGuy::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(isdeath==0){
        painter->drawPixmap(-pix.width() * 0.5, -pix.height() * 0.5, pix);
    }
    else if(isdeath==1){
        painter->drawPixmap(-deathlist[0].width() * 0.5, -deathlist[0].height() * 0.5, deathlist[i]);
        painter->drawPixmap(-deathbloodlist[0].width() * 0.5, -deathbloodlist[0].height() * 0.5,
                           deathbloodlist[i]);
        painter->drawPixmap(-oldguydeadbody.width() * 0.5, -oldguydeadbody.height() * 0.5,
                           oldguydeadbody);
    }
    else if(isdeath==2){
        painter->drawPixmap(-oldguydeadbody.width() * 0.5, -oldguydeadbody.height() * 0.5, oldguydeadbody);

    }
}

void OldGuy::updateDeathAnimation()
{

    if(isdeath==1){
        qDebug()<<"i="<<i;
        if(i==5){
            i=0;
            isdeath=2;
        }
        else{
            i++;
        }
        this->update();
    }
}

void OldGuy::setState(double ro)
{
    if(state_time==0){
        rotate = ro;
        if(rotate>-90&&rotate<90){
            state_index1=0;
            position = this->mapToScene(30,7);
            this->setRotation(rotate);
        }
        else{
            state_index1=1;
            position = this->mapToScene(-30,7);
            this->setRotation(180+rotate);
        }
        state_index2 = (state_index2+1)%3;
        pix = QPixmap(root + "/res/old_guy_frame" + state[state_index1][state_index2]).scaled(85, 95);
        this->update();
    }
}

void OldGuy::Shooting()
{
    shootrecord ++;
    if(shootrecord>shootspeed){
        shootrecord=0;
        for(int j=0;j<bulletlist.size();j++){
            if(!bulletlist[j]->isVisible()){
                bulletlist[j]->setPos(position);
                bulletlist[j]->setRotation(rotate);
                bulletlist[j]->setVisible(true);
                break;
            }
        }
    }
}

